package com.xiaoyun.alllearning.domain.common;

import com.xiaoyun.alllearning.exception.ErrorCodeEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class ResponseResult<T> implements Serializable {
    /**
     * 是否成功
     */
    private Boolean success;
    /**
     * 编码
     */
    private String code;
    /**
     * 描述信息
     */
    private String message;
    /**
     * 结果
     */
    private T result;

    public static <T> ResponseResult success(T result) {
        ResponseResult<T> responseResult = new ResponseResult<>();

        responseResult.setSuccess(Boolean.TRUE);
        responseResult.setResult(result);

        return responseResult;
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseResult failure(String code, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();

        responseResult.setSuccess(Boolean.FALSE);
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T> ResponseResult failure(ErrorCodeEnum errorCodeEnum) {
        return failure(errorCodeEnum.getCode(), errorCodeEnum.getMessage());
    }
}
